3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
24 * Resort normal UTF-8 order by putting a bunch of stuff in PUA
26 * This takes a bunch of characters (The alphabet) that should,
27 * be together, and converts them all to private-use-area characters
28 * so that they are all sorted in the right order relative to each
31 * This renumbers characters starting at U+F3000 (Chosen to avoid
32 * conflicts with other people using private use area)
34 * This does not support fancy things like secondary differences, etc.
35 * (It supports digraphs, trigraphs etc. though.)
37 * It is expected most people will subclass this and just override the
38 * constructor to hard-code an alphabet.
40 class CustomUppercaseCollation
extends NumericUppercaseCollation
{
42 /** @var array $alphabet Sorted array of letters */
45 /** @var array $puaSubset List of private use area codes */
49 * @note This assumes $alphabet does not contain U+F3000-U+F3FFF
51 * @param array $alphabet Sorted array of uppercase characters.
52 * @param Language $lang What language for number sorting.
54 public function __construct( array $alphabet, Language
$lang ) {
55 if ( count( $alphabet ) < 1 ||
count( $alphabet ) >= 4096 ) {
56 throw new UnexpectedValueException( "Alphabet must be < 4096 items" );
58 $this->firstLetters
= $alphabet;
59 // For digraphs, only the first letter is capitalized in input
60 $this->alphabet
= array_map( [ $lang, 'uc' ], $alphabet );
62 $this->puaSubset
= [];
63 $len = count( $alphabet );
64 for ( $i = 0; $i < $len; $i++
) {
65 $this->puaSubset
[] = "\xF3\xB3" . chr( floor( $i / 64 ) +
128 ) . chr( ( $i %
64 ) +
128 );
68 // Sort these arrays so that any trigraphs, digraphs etc. are first
69 // (and they get replaced first in convertToPua()).
70 $lengths = array_map( 'mb_strlen', $this->alphabet
);
71 array_multisort( $lengths, SORT_DESC
, $this->firstLetters
, $this->alphabet
, $this->puaSubset
);
73 parent
::__construct( $lang );
76 private function convertToPua( $string ) {
77 return str_replace( $this->alphabet
, $this->puaSubset
, $string );
80 public function getSortKey( $string ) {
81 return $this->convertToPua( parent
::getSortKey( $string ) );
84 public function getFirstLetter( $string ) {
85 $sortkey = $this->getSortKey( $string );
87 // In case a title begins with a character from our alphabet, return the corresponding
88 // first-letter. (This also happens if the title has a corresponding PUA code in it, to avoid
89 // inconsistent behaviour. This class mostly assumes that people will not use PUA codes.)
90 $index = array_search( substr( $sortkey, 0, 4 ), $this->puaSubset
);
91 if ( $index !== false ) {
92 return $this->firstLetters
[ $index ];
95 // String begins with a character outside of our alphabet, fall back
96 return parent
::getFirstLetter( $string );